<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;

class Role extends Base
{
    /**
     * 角色列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function RoleList(){
        //记录数量
        $Count = Db::name('admin_role')->count();
        $this->assign('Count',$Count);

        //管理员信息
        $RoleList = Db::name('admin_role')->select();
        $this->assign('RoleList',$RoleList);

        return $this->fetch('RoleList');
    }

    /**
     * 管理员添加
     * @return array|mixed
     */
    public function RoleAdd(){
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            $Data['add_time'] = time(); //添加时间
            //向表中添加数据
            if(Db::name('admin_role')->insert($Data)){
                return ['status'=>1,'message'=>'添加成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }

        //权限信息
        $ParentList = Db::name('admin_menu')->where(['parent_id'=>0,'is_show'=>1])->select();
        foreach ($ParentList as $k=>$v){
            $ParentList[$k]['SonList'] = Db::name('admin_menu')->where(['parent_id'=>$ParentList[$k]['id'],'is_show'=>1])->select();
        }
        $this->assign('ParentList',$ParentList);

        return $this->fetch('RoleAdd');
    }


    /**
     * 角色编辑
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function RoleEdit(){
        $RoleInfo = Db::name('admin_role')->where(['id'=>Param('id')])->find();
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            $Data['add_time'] = time(); //添加时间
            //向表中添加数据
            if(Db::name('admin_role')->update($Data)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        $this->assign('RoleInfo',$RoleInfo);

        $RolePower = explode(',',$RoleInfo['power']);
        //父级菜单
        $ParentList = Db::name('admin_menu')->where(['parent_id'=>0])->select();
        foreach ($ParentList as $k=>$v){
            if(in_array($ParentList[$k]['id'],$RolePower)){
                $ParentList[$k]['status'] = 1;
            }else{
                $ParentList[$k]['status'] = 0;
            }
            //子级菜单
            $where['parent_id'] = $ParentList[$k]['id'];
            $ParentList[$k]['sonList'] = Db::name('admin_menu')->where($where)->select();
            foreach ($ParentList[$k]['sonList'] as $kk=>$vv){
                if(in_array($ParentList[$k]['sonList'][$kk]['id'],$RolePower)){
                    $ParentList[$k]['sonList'][$kk]['status'] = 1;
                }else{
                    $ParentList[$k]['sonList'][$kk]['status'] = 0;
                }
            }
        }
        $this->assign('ParentList',$ParentList);

        return $this->fetch('RoleEdit');
    }

    /**
     * 角色删除
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function RoleDelete(){
        //Post请求操作
        if(IsPost()){
            //删除表中数据
            if(Db::name('admin_role')->where(['id'=>Param('id')])->delete()){
                Db::name('admin')->where(['role'=>Param('id')])->update(['role'=>0]);
                return ['status'=>1,'message'=>'删除成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
    }
}